package com.antonauoja.javaril;

import java.util.ArrayList;

class SendRequest extends ApiRequest {

	String request = "";
	
	@Override
	String getRequestURL() {
		return String.format(SEND,
				cred.getUsername(),
				cred.getPassword(),
				cred.getApiKey(),
				request);
	}

	public void setRequest(SendType type, ArrayList<RilItem> list) {
		
		switch (type) { 
		case NEW:
			request += "new={";
			for (int i = 0; i < list.size(); i++) {
				request += "\"" + i + "\":{";
				request += "\"url\":\"" + list.get(i).getUrl() + "\",";
				request += "\"title\":\"" + list.get(i).getTitle() + "\"}";
				if(i == list.size()-1) {
					request += "}";
				} else {
					request += ",";
	
				}
			}
			break;
		case READ:
			request += "read={";
			for (int i = 0; i < list.size(); i++) {
				request += "\"" + i + "\":{";
				request += "\"url\":\"" + list.get(i).getUrl() + "\"}";
				if(i == list.size()-1) {
					request += "}";
				} else {
					request += ",";
	
				}
			}
			break;
		case UPDATE_TITLE:			
			request += "update_title={";
			for (int i = 0; i < list.size(); i++) {
				request += "\"" + i + "\":{";
				request += "\"url\":\"" + list.get(i).getUrl() + "\",";
				request += "\"title\":\"" + list.get(i).getTitle() + "\"}";
				if(i == list.size()-1) {
					request += "}";
				} else {
					request += ",";
	
				}
			}
			break;
		}
		
		request = cleanJSON(request);
		System.out.println(request);
	}

	private String cleanJSON(String in) {
		return in.replaceAll("\\s", "%20");
	}
}
